<template>
  <el-dialog :title="!dataForm.id ? '套餐新增' : '套餐修改'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="套餐名称" prop="title">
            <el-input v-model="dataForm.title" placeholder="套餐名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="创建时间" prop="createTime">
            <el-input v-model="dataForm.createTime" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="套餐形式" prop="setmealModel">
            <el-radio-group v-model="dataForm.setmealModel" @change="setmealModelChangeHandle" :disabled="dataForm.id != 0">
              <el-radio :label="1">单一优惠方式</el-radio>
              <el-radio :label="2">多种优惠方式</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="生效时间" prop="startTime">
            <el-date-picker style="width: 100%;" v-model="dataForm.startTime" type="date" value-format="yyyy-MM-dd"
              placeholder="选择日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="截至时间" prop="endTime">
            <el-date-picker style="width: 100%;" v-model="dataForm.endTime" type="date" value-format="yyyy-MM-dd"
              placeholder="选择日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="套餐类型" prop="type">
            <el-select v-model="dataForm.type" placeholder="请选择套餐类型" :disabled="dataForm.id != 0">
              <el-option label="框镜销售" value="4"></el-option>
              <el-option label="隐形销售" value="3"></el-option>
              <el-option label="辅料销售" value="5"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="套餐价格区间">
            <el-col :span="11">
              <!-- <el-input v-model="dataForm.setmealPriceMin"></el-input> -->
              {{dataForm.setmealPriceMin}}
            </el-col>
            <el-col :span="2">至</el-col>
            <el-col :span="11">
              <!-- <el-input v-model="dataForm.setmealPriceMax"></el-input> -->
              {{dataForm.setmealPriceMax}}
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="套餐总金额">
            {{dataForm.setmealPrice}}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="允许累计积分" prop="allowIntegral">
            <el-radio-group v-model="dataForm.allowIntegral">
              <el-radio :label="1">允许</el-radio>
              <el-radio :label="0">不允许</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="允许累加" prop="allowCumulative">
            <el-radio-group v-model="dataForm.allowCumulative">
              <el-radio :label="1">允许</el-radio>
              <el-radio :label="0">不允许</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="折上折" prop="allowDiscount">
            <el-radio-group v-model="dataForm.allowDiscount">
              <el-radio :label="1">允许</el-radio>
              <el-radio :label="0">不允许</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="21">
          <el-form-item label="查询部门">
            <el-input type="textarea" v-model="dataForm.deptName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label-width="0">
            <el-button size="mini" @click="openDeptDialog">选择</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input type="textarea" v-model="dataForm.memo" placeholder="备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-tabs type="border-card" v-if="dataForm.setmealModel == 1">
        <el-tab-pane label="单一优惠方式">
          <el-divider content-position="left">商品信息</el-divider>
          <el-row>
            <el-col :span="12">
              <el-form-item label="整单满">
                <el-col :span="11">
                  <el-input v-model="dataForm.totalPriceMin" placeholder="整单满最小值"></el-input>
                </el-col>
                <el-col :span="2">至</el-col>
                <el-col :span="11">
                  <el-input v-model="dataForm.totalPriceMax" placeholder="整单满最大值"></el-input>
                </el-col>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-col :span="9">
                <el-form-item label="优惠方式">
                  <el-select v-model="dataForm.preferentialWay">
                    <el-option label="打折" :value="2"></el-option>
                    <el-option label="返现" :value="3"></el-option>
                    <el-option label="特价" :value="4"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-row>
                  <el-col :span="8" v-if="dataForm.preferentialWay == 2">
                    <el-input v-model="dataForm.preferentialWayInfo" placeholder="折扣率" :disabled="dataForm.preferentialWay != 2"></el-input>
                  </el-col>
                  <el-col :span="8" v-if="dataForm.preferentialWay == 3">
                    <el-input v-model="dataForm.preferentialWayInfo" placeholder="返现金额" :disabled="dataForm.preferentialWay != 3"></el-input>
                  </el-col>
                  <el-col :span="8" v-if="dataForm.preferentialWay == 4">
                    <el-input v-model="dataForm.preferentialWayInfo" placeholder="特价金额" :disabled="dataForm.preferentialWay != 4"></el-input>
                  </el-col>
                </el-row>
              </el-col>
            </el-col>
          </el-row>
          <el-divider content-position="left">购买商品</el-divider>
          <el-row>
            <el-col :span="12">
              <el-button type="primary" icon="el-icon-plus" @click.prevent="addRow('buy')">添加购买商品</el-button>
              <el-button type="primary" icon="el-icon-delete" @click.prevent="delData('buy')">删除</el-button>
              <!-- <el-button class="el-icon-plus"></el-button> -->
              <!-- <el-button type="primary" @click="">批量添加商品</el-button> -->
              <!-- <el-button class="el-icon-minus" @click.prevent="delData()"></el-button> -->
            </el-col>
            <el-col :span="12">
              <el-form-item label="购买方式">
                <el-radio-group v-model="dataForm.buyMode">
                  <el-radio label="1">任选其一</el-radio>
                  <el-radio label="2">符合全部</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="avue-crud">
            <el-table :data="buyGoodsList" border @selection-change="handleSelectionChange1">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="ctgId" label="商品类别">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.ctgId">
                    <el-option v-for="item in ctgOption" :value="item.id" :label="item.name" :key="item.id"></el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="supplyName" label="商品制造商" width="200">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.supplyName" readonly>
                    <el-button slot="append" type="primary" @click="openDialog('supply',scope.row)">选择</el-button>
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column prop="brandName" label="商品品种" width="200">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.brandName" readonly>
                    <el-button slot="append" type="primary" @click="openDialog('brand',scope.row)">选择</el-button>
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column prop="viewGoodsName" label="商品名称" width="300">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.viewGoodsName" readonly>
                    <el-button slot="append" type="primary" @click="addGoodsBySingle(scope.row,'buy')">选择</el-button>
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column prop="price" label="商品原价区间"></el-table-column>
              <el-table-column prop="setmealPrice" label="套餐价格区间" width="240">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.setmealPriceMin" style="width: 100px;"></el-input>-<el-input v-model="scope.row.setmealPriceMax"
                    style="width: 100px;"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="goodsQuantity" label="销售数量">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.goodsQuantity"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="consumerPrices" label="消费满" width="240">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.consumerPricesMin" style="width: 100px;"></el-input>至<el-input v-model="scope.row.consumerPricesMax"
                    style="width: 100px;"></el-input>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
      <el-tabs type="border-card" v-if="dataForm.setmealModel == 2">
        <el-tab-pane label="多种优惠方式">
          <el-divider content-position="left">商品信息</el-divider>
          <el-row>
            <el-col :span="8">
              <el-form-item label="整单满">
                <el-col :span="11">
                  <el-input v-model="dataForm.totalPriceMin" placeholder="整单满最小值"></el-input>
                </el-col>
                <el-col :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-input v-model="dataForm.totalPriceMax" placeholder="整单满最大值"></el-input>
                </el-col>
              </el-form-item>
            </el-col>
          </el-row>

          <el-divider content-position="left">购买商品</el-divider>
          <el-row>
            <el-col :span="12">
              <el-button type="primary" icon="el-icon-plus" @click.prevent="addRow('buy')">添加购买商品</el-button>
              <el-button type="primary" icon="el-icon-delete" @click.prevent="delData('buy')">删除</el-button>
              <!-- <el-button class="el-icon-plus" @click.prevent="addRow('second')"></el-button> -->
              <!-- <el-button type="primary" @click="">批量添加商品</el-button> -->
              <!-- <el-button class="el-icon-minus" @click.prevent="delData()"></el-button> -->
            </el-col>
            <el-col :span="12">
              <el-form-item label="购买方式">
                <el-radio-group v-model="dataForm.buyMode">
                  <el-radio label="1">任选其一</el-radio>
                  <el-radio label="2">符合全部</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="avue-crud">
            <el-table :data="buyGoodsList" border @selection-change="handleSelectionChange1">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="ctgId" label="商品类别">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.ctgId">
                    <el-option v-for="item in ctgOption" :value="item.id" :label="item.name" :key="item.id"></el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="viewGoodsName" label="商品名称" width="300">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.viewGoodsName" readonly>
                    <el-button slot="append" type="primary" @click="addGoodsBySingle(scope.row,'buy')">选择</el-button>
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column prop="price" label="商品原价区间"></el-table-column>
              <el-table-column prop="setmealPrice" label="套餐价格区间" width="240">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.setmealPriceMin" style="width: 100px;"></el-input>-<el-input v-model="scope.row.setmealPriceMax"
                    style="width: 100px;"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="goodsQuantity" label="销售数量">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.goodsQuantity"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="consumerPrices" label="消费满" width="240">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.consumerPricesMin" style="width: 100px;"></el-input>至<el-input v-model="scope.row.consumerPricesMax"
                    style="width: 100px;"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="preferentialWay" label="优惠方式">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.preferentialWay">
                    <el-option label="原价" :value="1"></el-option>
                    <el-option label="打折" :value="2"></el-option>
                    <el-option label="返现" :value="3"></el-option>
                    <el-option label="特价" :value="4"></el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="XXX" label="套餐单价"></el-table-column>
              <el-table-column prop="deptName" label="特价金额">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.consumerPricesMin" :disabled="scope.row.preferentialWay != 2"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="deptName" label="优惠金额" v-if="dataForm.setmealModel == 1">
                <template slot-scope="scope" v-if="dataForm.setmealModel == 1">
                  <el-input v-model="scope.row.consumerPricesMin" :disabled="scope.row.preferentialWay != 3"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="deptName" label="优惠折扣" v-if="dataForm.setmealModel == 1">
                <template slot-scope="scope" v-if="dataForm.setmealModel == 1">
                  <el-input v-model="scope.row.consumerPricesMin" :disabled="scope.row.preferentialWay != 4"></el-input>
                </template>
              </el-table-column>
            </el-table>
          </div>

          <el-divider content-position="left">优惠商品</el-divider>
          <el-row>
            <el-col :span="12">
              <el-button type="primary" icon="el-icon-plus" @click.prevent="addRow('discount')">添加消费抵扣商品</el-button>
              <el-button type="primary" icon="el-icon-delete" @click.prevent="delData('discount')">删除</el-button>
            </el-col>
            <el-col :span="12">
              <el-form-item label="购买方式">
                <el-radio-group v-model="dataForm.discountMode">
                  <el-radio label="1">任选其一</el-radio>
                  <el-radio label="2">符合全部</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="avue-crud">
            <el-table :data="discountGoodsList" border @selection-change="handleSelectionChange2">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="ctgId" label="商品类别">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.ctgId">
                    <el-option v-for="item in ctgOption" :value="item.id" :label="item.name" :key="item.id"></el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="viewGoodsName" label="商品名称" width="300">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.viewGoodsName" readonly>
                    <el-button slot="append" type="primary" @click="addGoodsBySingle(scope.row,'discount')">选择</el-button>
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column prop="price" label="商品原价区间"></el-table-column>
              <el-table-column prop="setmealPrice" label="套餐价格区间" width="240">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.setmealPriceMin" style="width: 100px;"></el-input>-<el-input v-model="scope.row.setmealPriceMax"
                    style="width: 100px;"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="goodsQuantity" label="销售数量">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.goodsQuantity"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="consumerPrices" label="消费满" width="240">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.consumerPricesMin" style="width: 100px;"></el-input>至<el-input v-model="scope.row.consumerPricesMax"
                    style="width: 100px;"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="preferentialWay" label="优惠方式">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.preferentialWay">
                    <el-option label="原价" :value="1"></el-option>
                    <el-option label="打折" :value="2"></el-option>
                    <el-option label="返现" :value="3"></el-option>
                    <el-option label="特价" :value="4"></el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="XXX" label="套餐单价"></el-table-column>
              <el-table-column prop="deptName" label="特价金额">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.consumerPricesMin" :disabled="scope.row.preferentialWay != 2"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="deptName" label="优惠金额">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.consumerPricesMin" :disabled="scope.row.preferentialWay != 3"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="deptName" label="优惠折扣">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.consumerPricesMin" :disabled="scope.row.preferentialWay != 4"></el-input>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
      <goods-list v-if="goodsListVisible" ref="sinleGoodsList" @updateForm="updateFormData"></goods-list>
    </el-form>
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
    <search-brand v-if="brandVisible" ref="brandDialog" @refreshData="getBrandData"></search-brand>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'Vuex'
  import { getCtgList } from '@/api/eims/ctg'
  import { getObj, addObj, putObj } from '@/api/eims/set-meal'

  import SearchDept from './SearchDept'
  import GoodsList from "./SearchGoods"
  import SearchSupply from './SearchSupply'
  import SearchBrand from './SearchBrand'

  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        deptVisible: false,
        dataList: [],
        buyGoodsList: [],
        discountGoodsList: [],
        dataForm: {
          id: 0,
          title: '',
          setmealModel: 1,
          type: '',
          setmealPriceMin: '',
          setmealPriceMax: '',
          originalPrice: '',
          setmealPrice: '',
          startTime: '',
          endTime: '',
          allowIntegral: 1,
          allowCumulative: 1,
          allowDiscount: 0,
          auditStatus: '',
          failureStatus: '',
          failureTime: '',
          disuseUser: '',
          preferentialWay: '',
          preferentialWayInfo: '',
          totalPriceMin: '',
          totalPriceMax: '',
          memo: '',
          createTime: '',
          deptIds: [],
          deptName: '',
          buyMode: '2',
          discountMode: '2',
          goodsBuyList: [],
          goodsCheapList: []
        },
        buySelectList: [],
        discountSelectList: [],
        selectlistRow: [],
        ctgOption: [],
        brandOption: [],
        goodsListVisible: false,
        supplyVisible: false,
        brandVisible: false,
        dataRule: {
          title: [{
            required: true,
            message: '套餐名称不能为空',
            trigger: 'blur'
          }],
          setmealModel: [{
            required: true,
            message: '套餐形式不能为空',
            trigger: 'blur'
          }],
          type: [{
            required: true,
            message: '套餐类型不能为空',
            trigger: 'blur'
          }],
          startTime: [{
            required: true,
            message: '生效时间不能为空',
            trigger: 'blur'
          }],
          endTime: [{
            required: true,
            message: '截止时间不能为空',
            trigger: 'blur'
          }],
          allowIntegral: [{
            required: true,
            message: '是否允许生成积分不能为空',
            trigger: 'blur'
          }],
          allowCumulative: [{
            required: true,
            message: '是否允许累加不能为空',
            trigger: 'blur'
          }],
          allowDiscount: [{
            required: true,
            message: '是否允许折上折不能为空',
            trigger: 'blur'
          }],
          preferentialWay: [{
            required: true,
            message: '优惠方式不能为空',
            trigger: 'blur'
          }],
          preferentialWayInfo: [{
            required: true,
            message: '折扣率/返现金额/特价金额不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo', 'bizData'])
    },
    components: {
      SearchDept,
      GoodsList,
      SearchSupply,
      SearchBrand
    },
    created() {
      this.timeFormate(new Date());
      this.getCtgList()
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        if (this.dataForm.id == 0) {
          this.buyGoodsList = []
          this.discountGoodsList = []
          for (let key in this.dataForm) {
            if (key == 'setmealModel') {
              this.dataForm[key] = 1
            } else if (key == 'allowIntegral') {
              this.dataForm[key] = 1
            } else if (key == 'allowCumulative') {
              this.dataForm[key] = 1
            } else if (key == 'allowDiscount') {
              this.dataForm[key] = 0
            } else if (key == 'discountMode') {
              this.dataForm[key] = 2
            } else if (key == 'deptIds' || key == 'goodsBuyList' || key == 'goodsCheapList') {
              this.dataForm[key] = []
            } else {
              this.dataForm[key] = ''
            }
          }
          this.timeFormate(new Date());
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataForm.deptName = response.data.data.deptNames.join(',')
                this.buyGoodsList = response.data.data.goodsBuyList
                this.discountGoodsList = response.data.data.goodsCheapList
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      //显示当前时间（年月日时分秒）
      timeFormate(timeStamp) {
        let year = new Date(timeStamp).getFullYear();
        let month =
          new Date(timeStamp).getMonth() + 1 < 10 ?
          "0" + (new Date(timeStamp).getMonth() + 1) :
          new Date(timeStamp).getMonth() + 1;
        let date =
          new Date(timeStamp).getDate() < 10 ?
          "0" + new Date(timeStamp).getDate() :
          new Date(timeStamp).getDate();
        let hh =
          new Date(timeStamp).getHours() < 10 ?
          "0" + new Date(timeStamp).getHours() :
          new Date(timeStamp).getHours();
        let mm =
          new Date(timeStamp).getMinutes() < 10 ?
          "0" + new Date(timeStamp).getMinutes() :
          new Date(timeStamp).getMinutes();
        let ss =
          new Date(timeStamp).getSeconds() < 10 ?
          "0" + new Date(timeStamp).getSeconds() :
          new Date(timeStamp).getSeconds();
        this.nowTime =
          "POUT" +
          year +
          "" +
          month +
          "" +
          date +
          "" +
          hh +
          "" +
          mm +
          "" +
          ss +
          this.dataForm.deptId;
        this.dataForm.createTime = year + "-" + month + "-" + date;
      },
      // 套餐形式改变
      setmealModelChangeHandle() {
        this.buyGoodsList = []
        this.buySelectList = []
        this.discountGoodsList = []
        this.discountSelectList = []
      },
      // 表单提交
      dataFormSubmit() {
        this.dataForm.goodsBuyList = this.buyGoodsList
        this.dataForm.goodsCheapList = this.discountGoodsList
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          } else {
            this.ischeck = false
          }
        })
      },
      //  打开查询部门弹窗
      openDeptDialog() {
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init()
        })
      },
      //  获取选择部门数据
      getDeptData(data) {
        let ids = []
        let names = []
        if (data.length) {
          data.forEach(item => {
            ids.push(item.id)
            names.push(item.name)
          })
        }
        this.dataForm.deptName = names.join(',')
        this.dataForm.deptIds = ids
      },
      // 选择事件 得到选中的数据
      handleSelectionChange(data) {
        this.selectlistRow = data
      },
      handleSelectionChange1(data) {
        this.buySelectList = data
      },
      handleSelectionChange2(data) {
        this.discountSelectList = data
      },
      addRow(type) {
        var timestamp = new Date().getTime()
        var list = {
          timestamp: timestamp,
          ctgId: 1,
          goodsName: '',
          price: '',
          setmealPriceMin: '',
          setmealPriceMax: '',
          goodsQuantity: '',
          consumerPricesMin: '',
          consumerPricesMax: '',
          preferentialWay: '',
        };
        switch (type) {
          case 'buy':
            {
              this.buyGoodsList.push(list)
              break;
            };
          case 'discount':
            {
              this.discountGoodsList.push(list)
              break;
            };
        }

      },
      //  删除
      delData(type) {
        switch (type) {
          case 'buy':
            {
              this.buySelectList.forEach((item, index) => {
                for (let i = 0; i < this.buyGoodsList.length; i++) {
                  if (this.buyGoodsList[i].timestamp === item.timestamp) {
                    this.buyGoodsList.splice(i, 1)
                  }
                }
                this.buySelectList = []
              })
              break;
            };
          case 'discount':
            {
              this.discountSelectList.forEach((item, index) => {
                for (let i = 0; i < this.discountGoodsList.length; i++) {
                  if (this.discountGoodsList[i].timestamp === item.timestamp) {
                    this.discountGoodsList.splice(i, 1)
                  }
                }
                this.discountSelectList = []
              })
              break;
            }
        }
      },
      //  获取商品类型列表
      getCtgList() {
        getCtgList().then(result => {
          this.ctgOption = result.data.data
        })
      },
      
      // 打开商品列表弹窗
      addGoodsBySingle(row, type) {
        // 判断是否选中制造商
        if (!row.ctgId) {
          this.$message.warning("请选择商品类别");
          return;
        }
        let params = {
          ctgId: row.ctgId,
          supplyId: row.supplyId,
          brandId: row.brandId
        };
        let data = {
          params: params,
          data: row
        };
        this.goodsListVisible = true;
        this.$nextTick(() => {
          this.$refs.sinleGoodsList.init(data, type);
        });
      },
      //更新表单数据
      updateFormData(data) {
        switch (data.type) {
          case 'buy':
            {
              this.buyGoodsList.forEach((item, index) => {
                if (item.timestamp == data.timestamp) {
                  this.buyGoodsList[index] = data
                }
              })
            };
          case 'discount':
            {
              this.discountGoodsList.forEach((item, index) => {
                if (item.timestamp == data.timestamp) {
                  this.buyGoodsList[index] = data
                }
              })
            }
        }
        // let list = []
        // data.forEach(item => {
        //   list.push({
        //     goodsId: item.id,
        //     ctgId: item.ctgId,
        //     viewGoodsName: item.viewGoodsName,
        //     price: item.price +" 至 "+ item.price,
        //     number: ''
        //   });
        // })
        // this.buyGoodsList = list
        // this.dataForm.goodsBuyList = this.buyGoodsList
      },
      //  打开弹窗
      openDialog(name,row) {
        if (name == 'supply') {
          if (!row.ctgId) {
            this.$message.warning('请先选择商品类别')
            return
          }
          let params = {
            ctgId: row.ctgId
          };
          let data = {
            params: params,
            data: row
          };
          this.supplyVisible = true
          this.$nextTick(() => {
            this.$refs.supplyDialog.init(data)
          })
        } else if (name == 'brand') {
          if (!row.supplyId) {
            this.$message.warning('请先选择所属制造商')
            return
          }
          let params = {
            ctgId: row.ctgId,
            supplyId: row.supplyId,
            timestamp: row.timestamp
          };
          let data = {
            params: params,
            data: row
          };
          this.brandVisible = true
          this.$nextTick(() => {
            this.$refs.brandDialog.init(data)
          })
        }
      },
      //  获取制造商数据
      getSupplyData(data) {
        console.log(data)
        this.buyGoodsList.forEach((item, index) => {
          if (item.timestamp == data.timestamp) {
            this.buyGoodsList[index] = data
            this.$set(this.buyGoodsList[index],'supplyName',data.supplyName)
          }
        })
      },
      //  获取商品品种信息
      getBrandData(data) {
        console.log(data)
        this.buyGoodsList.forEach((item, index) => {
          if (item.timestamp == data.timestamp) {
            this.buyGoodsList[index] = data
          }
        })
      },
    }
  }
</script>

<style>
  .el-border {
    width: 300px;
    height: 60px;
    border: solid 1px #000000;
    position: relative;
  }

  .border-title {
    position: absolute;
    left: 30px;
    top: -10px;
    background-color: #fff;
  }
</style>
